package com.example.petstore_2.controller;

import com.example.petstore_2.common.CommonResponse;
import com.example.petstore_2.domain.Item;
import com.example.petstore_2.domain.Product;
import com.example.petstore_2.service.ItemService;
import com.example.petstore_2.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ItemService itemService;


    /**
     * 获取全部products
     * @return
     */
    @GetMapping("/products")
    public CommonResponse getAllProducts(){
        return productService.getProductList();
    }

    /**
     * 根据productid找product
     * @param productId
     * @return
     */
    @GetMapping("/products/{productId}")
    public CommonResponse getProductById(@PathVariable("productId") String productId){
        return productService.getProductByProductId(productId);
    }


    /**
     * 根据category获取products
     * @param category
     * @return
     */
    @GetMapping("/products/category/{category}")
    public CommonResponse getProductListByCategory(@PathVariable("category") String category){
        return productService.getProductListByCategory(category);
    }

    /**
     * 关键字搜索
     * @param keyWord
     * @return
     */
    @GetMapping("/products/keyWords/{keyWords}")
    public CommonResponse getProductListByKey(@PathVariable("keyWords") String keyWord){
        return productService.getProductListByKey(keyWord);
    }

    /**
     * 根据itemid获取对应product
     * @param itemId
     * @return
     */
    @GetMapping("/products/itemId/{itemId}")
    public CommonResponse getProductByItemId(@PathVariable("itemId") String itemId){
        CommonResponse commonResponse = itemService.getItemByItemid(itemId);
        Item item = (Item) commonResponse.getData();
        return productService.getProductByProductId(item.getProductid());
    }
}
